// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{

    /// <summary />
    partial class ProductMapper
    {

        /// <summary />
        static ProductMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Product> _FindAll(DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(Resources.Sql.ProductSelectAll);

            return Load(commands, dataMappers);
        }

        private Product.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Product.DataTransfer data=new Product.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Product.DataTransfer FillData(Product.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("ProductID");
            if (!record.IsDBNull(ord))
                data.ProductID=record.GetInt32(ord);

            ord=record.GetOrdinal("CategoryID");
            if (!record.IsDBNull(ord))
                data.CategoryID=record.GetInt32(ord);

            ord=record.GetOrdinal("SupplierID");
            if (!record.IsDBNull(ord))
                data.SupplierID=record.GetInt32(ord);

            ord=record.GetOrdinal("ProductName");
            if (!record.IsDBNull(ord))
                data.ProductName=record.GetString(ord);

            ord=record.GetOrdinal("QuantityPerUnit");
            if (!record.IsDBNull(ord))
                data.QuantityPerUnit=record.GetString(ord);

            ord=record.GetOrdinal("UnitPrice");
            if (!record.IsDBNull(ord))
                data.UnitPrice=record.GetDecimal(ord);

            ord=record.GetOrdinal("UnitsInStock");
            if (!record.IsDBNull(ord))
                data.UnitsInStock=record.GetInt16(ord);

            ord=record.GetOrdinal("UnitsOnOrder");
            if (!record.IsDBNull(ord))
                data.UnitsOnOrder=record.GetInt16(ord);

            ord=record.GetOrdinal("ReorderLevel");
            if (!record.IsDBNull(ord))
                data.ReorderLevel=record.GetInt16(ord);

            ord=record.GetOrdinal("Discontinued");
            if (!record.IsDBNull(ord))
                data.Discontinued=record.GetBoolean(ord);

            return data;
        }

        /// <summary />
        private static DbCommand _CreateSelectCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Product.PrimaryKey key)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ProductSelect,
                    parameterBuilder.GetMarker("ProductID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateDeleteCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Product.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ProductDelete,
                    parameterBuilder.GetMarker("ProductID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ProductID
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateInsertCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Product.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ProductInsert,
                    parameterBuilder.GetMarker("CategoryID", 1),
                    parameterBuilder.GetMarker("SupplierID", 2),
                    parameterBuilder.GetMarker("ProductName", 3),
                    parameterBuilder.GetMarker("QuantityPerUnit", 4),
                    parameterBuilder.GetMarker("UnitPrice", 5),
                    parameterBuilder.GetMarker("UnitsInStock", 6),
                    parameterBuilder.GetMarker("UnitsOnOrder", 7),
                    parameterBuilder.GetMarker("ReorderLevel", 8),
                    parameterBuilder.GetMarker("Discontinued", 9)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CategoryID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.CategoryID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("SupplierID", 2),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.SupplierID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductName", 3),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.ProductName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("QuantityPerUnit", 4),
                DbType.String,
                20,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.QuantityPerUnit ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitPrice", 5),
                DbType.Decimal,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitPrice
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitsInStock", 6),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitsInStock
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitsOnOrder", 7),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitsOnOrder
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ReorderLevel", 8),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ReorderLevel
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Discontinued", 9),
                DbType.Boolean,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Discontinued
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateUpdateCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Product.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.ProductUpdate,
                    parameterBuilder.GetMarker("CategoryID", 1),
                    parameterBuilder.GetMarker("SupplierID", 2),
                    parameterBuilder.GetMarker("ProductName", 3),
                    parameterBuilder.GetMarker("QuantityPerUnit", 4),
                    parameterBuilder.GetMarker("UnitPrice", 5),
                    parameterBuilder.GetMarker("UnitsInStock", 6),
                    parameterBuilder.GetMarker("UnitsOnOrder", 7),
                    parameterBuilder.GetMarker("ReorderLevel", 8),
                    parameterBuilder.GetMarker("Discontinued", 9),
                    parameterBuilder.GetMarker("ProductID", 10)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CategoryID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.CategoryID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("SupplierID", 2),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.SupplierID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductName", 3),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.ProductName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("QuantityPerUnit", 4),
                DbType.String,
                20,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.QuantityPerUnit ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitPrice", 5),
                DbType.Decimal,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitPrice
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitsInStock", 6),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitsInStock
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitsOnOrder", 7),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitsOnOrder
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ReorderLevel", 8),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ReorderLevel
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Discontinued", 9),
                DbType.Boolean,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Discontinued
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductID", 10),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ProductID
            );

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected Salamanca.DataAccess.Data.ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private Salamanca.DataAccess.Data.ParameterBuilder _ParameterBuilder;

    }

}
